package org.jeecg.modules.solitaire_app.app.controller;

import java.util.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.solitaire_app.app.entity.YsyOilType;
import org.jeecg.modules.solitaire_app.app.entity.YsyRefineryChg;
import org.jeecg.modules.solitaire_app.app.entity.YsyRefineryOil;
import org.jeecg.modules.solitaire_app.app.entity.YsyRefineryPrice;
import org.jeecg.modules.solitaire_app.app.service.IYsyOilTypeService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.solitaire_app.app.service.IYsyRefineryOilService;
import org.jeecg.modules.solitaire_app.app.service.IYsyRefineryPriceService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 油品类型管理
 * @Author: jeecg-boot
 * @Date: 2024-03-25
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "油品类型管理")
@RestController
@RequestMapping("/app/ysyOilType")
public class YsyOilTypeController extends JeecgController<YsyOilType, IYsyOilTypeService> {
    @Autowired
    private IYsyOilTypeService ysyOilTypeService;
    @Autowired
    private IYsyRefineryPriceService ysyRefineryPriceService;
    @Autowired
    private IYsyRefineryOilService ysyRefineryOilService;

    /**
     * 分页列表查询
     *
     * @param ysyOilType
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "油品类型管理-分页列表查询")
    @ApiOperation(value = "油品类型管理-分页列表查询", notes = "油品类型管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(YsyOilType ysyOilType,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<YsyOilType> queryWrapper = QueryGenerator.initQueryWrapper(ysyOilType, req.getParameterMap());
        Page<YsyOilType> page = new Page<YsyOilType>(pageNo, pageSize);
        queryWrapper.orderByDesc("weight");
        IPage<YsyOilType> pageList = ysyOilTypeService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 查询炼厂有哪些油品
     *
     * @return
     */
    @AutoLog(value = "炼厂表-查询炼厂有哪些油品")
    @ApiOperation(value = "炼厂表-查询炼厂有哪些油品", notes = "炼厂表-查询炼厂有哪些油品")
    @GetMapping(value = "/getAllOilTypeRefinery/{refineryId}")
    public Result<List<YsyOilType>> getAllOilTypeRefinery(@PathVariable String refineryId) {
        return ysyOilTypeService.getAllOilTypeRefinery(refineryId);
    }

    /**
     * 添加
     *
     * @param ysyOilType
     * @return
     */
    @AutoLog(value = "油品类型管理-添加")
    @ApiOperation(value = "油品类型管理-添加", notes = "油品类型管理-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody YsyOilType ysyOilType) {
        ysyOilTypeService.save(ysyOilType);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param ysyOilType
     * @return
     */
    @AutoLog(value = "油品类型管理-编辑")
    @ApiOperation(value = "油品类型管理-编辑", notes = "油品类型管理-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody YsyOilType ysyOilType) {
        ysyOilTypeService.updateById(ysyOilType);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "油品类型管理-通过id删除")
    @ApiOperation(value = "油品类型管理-通过id删除", notes = "油品类型管理-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        ysyOilTypeService.removeById(id);
        //删除关联的炼厂报价
        ysyRefineryPriceService.remove(Wrappers.<YsyRefineryPrice>lambdaQuery()
                .eq(YsyRefineryPrice::getYsyOilTypeId, id));
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "油品类型管理-批量删除")
    @ApiOperation(value = "油品类型管理-批量删除", notes = "油品类型管理-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.ysyOilTypeService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 炼厂绑定油品
     *
     * @param map
     * @return
     */
    @AutoLog(value = "油品类型管理-炼厂绑定油品")
    @ApiOperation(value = "油品类型管理-炼厂绑定油品", notes = "油品类型管理-炼厂绑定油品")
    @PostMapping(value = "/bandOitTypeList")
    public Result<?> bandOitTypeList(@RequestBody Map<String, Object> map) {
        String ysyOitTypeId = map.get("ysyOilTypeId").toString();
        String ysyRefineryId = map.get("ysyRefineryId").toString();

        YsyRefineryOil ysyRefineryOil = ysyRefineryOilService.getOne(Wrappers.<YsyRefineryOil>lambdaQuery()
                .eq(YsyRefineryOil::getOilTypeId, ysyOitTypeId).eq(YsyRefineryOil::getRefineryId, ysyRefineryId));

        if (ObjectUtil.isEmpty(ysyRefineryOil)) {
            YsyRefineryOil ysyRefineryOilInsert = new YsyRefineryOil();
            ysyRefineryOilInsert.setOilTypeId(ysyOitTypeId);
            ysyRefineryOilInsert.setRefineryId(ysyRefineryId);
            ysyRefineryOilInsert.setCreateTime(new Date());
            ysyRefineryOilService.save(ysyRefineryOilInsert);
        }

        return Result.ok("绑定成功");
    }

    /**
     * 解绑油品
     *
     * @param map
     * @return
     */
    @AutoLog(value = "油品类型管理-解绑油品")
    @ApiOperation(value = "油品类型管理-解绑油品", notes = "油品类型管理-解绑油品")
    @PostMapping(value = "/cancelBandOitTypeList")
    public Result<?> cancelBandOitTypeList(@RequestBody Map<String, Object> map) {
        String ysyOitTypeId = map.get("ysyOilTypeId").toString();
        String ysyRefineryId = map.get("ysyRefineryId").toString();
        //删除报价
        ysyRefineryPriceService.remove(Wrappers.<YsyRefineryPrice>lambdaQuery()
                .eq(YsyRefineryPrice::getYsyRefineryId, ysyRefineryId)
                .eq(YsyRefineryPrice::getYsyOilTypeId, ysyOitTypeId));
        //解绑
        ysyRefineryOilService.remove(Wrappers.<YsyRefineryOil>lambdaQuery()
                .eq(YsyRefineryOil::getOilTypeId,ysyOitTypeId)
                .eq(YsyRefineryOil::getRefineryId,ysyRefineryId));
        return Result.ok("已解绑");
    }


    /**
     * 查询炼厂的油品
     *
     * @param refineryId
     * @return
     */
    @AutoLog(value = "油品类型管理-查询炼厂的油品")
    @ApiOperation(value = "油品类型管理-查询炼厂的油品", notes = "油品类型管理-查询炼厂的油品")
    @GetMapping(value = "/getOilTypeByRefineryId/{refineryId}")
    public Result<?> getOilTypeByRefineryId(@PathVariable String refineryId) {
        List<YsyRefineryOil> refineryPrices = ysyRefineryOilService.list(Wrappers.<YsyRefineryOil>lambdaQuery()
                .eq(YsyRefineryOil::getRefineryId, refineryId).groupBy(YsyRefineryOil::getOilTypeId));

        List<YsyOilType> ysyOilTypes = new ArrayList<>();
        refineryPrices.stream().forEach(item -> {
            YsyOilType ysyOilType = ysyOilTypeService.getById(item.getOilTypeId());
            ysyOilTypes.add(ysyOilType);
        });

        return Result.ok(ysyOilTypes);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "油品类型管理-通过id查询")
    @ApiOperation(value = "油品类型管理-通过id查询", notes = "油品类型管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        YsyOilType ysyOilType = ysyOilTypeService.getById(id);
        return Result.ok(ysyOilType);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param ysyOilType
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YsyOilType ysyOilType) {
        return super.exportXls(request, ysyOilType, YsyOilType.class, "油品类型管理");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YsyOilType.class);
    }

}
